
define(function(require) {
	var $ = require("jquery");
	var utils = require("utils");
	var backManagerService = require("bean/backManagerService");
	var ctx = utils.constants.ctx;	
	var table = layui.table;
	var form = layui.form;
	var layer = layui.layer;
	var element = layui.element;
	var table = layui.table;
	var ui = require("ui");
	
	var tablelin = table.render({
	    elem: '#demo'
	    ,url : '/back/base/userIdentityData'
	    ,cols: [[ //标题栏
	      {checkbox: true}
	      ,{field: 'name', title: '名称', templet:'#Isdefault',width: 220}
	      ,{field: 'initScore', title: '初始积分', width: 280}
	      ,{field: '', title: '操作', templet:'#barDemo', width: 300}
	    ]] 
	    ,even: true
	    ,page: true //是否显示分页
	    ,limits: [8, 12, 15,18]
	    ,limit: 8 //每页默认显示的数量
	    ,id: "iden_table"
	  });
	
	//重载表格事件
	$(document).on("click","#searchBtn",function(){		
		var param = {
				name:$("#search_input").val()
		}
		tablelin.reload({
			where:param
		});
	})
	
	//添加
	$(document).on("click","#addUserIdentity",function(){
		
		$.post('/back/base/userIdentityEditUI', function(str){
			  layer.open({			  
			    type: 1,
			    content: str ,
			    title:'添加用户身份'//注意，如果str是object，那么需要字符拼接。
			  });
			  form.render();
		});
		
	})
	
	//删除
	$(document).on("click","#deteleUserIdentity",function(){
		 var checkStatus = layui.table.checkStatus('iden_table');
			if (checkStatus.data.length===0) {
				layer.alert("请选择至少一个身份");
				return;
			}
			layer.confirm("确定要删除这些身份？",{icon: 3, title:'提示'},function(isOk){
				if(isOk){
					var data = checkStatus.data;
					var array = [];
					for(var i = 0; i<data.length;i++){
						array.push(data[i].id);   
					}
					if (array.length != 0) {
						try{
							ui.mask().show("请稍候...");
							backManagerService.userIdentityDelete(array);
							ui.mask().hide();
							layer.alert("删除成功",function(){
								
								window.location.reload(true);
							});
						} catch (e) {
							ui.mask().hide();
							layer.alert("删除失败:"+e.message);
						}
					}
				}
			})
	})
	
	
	table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		  var layEvent = obj.event; //获得 lay-event 对应的值
		  var data = obj.data;
		  var id = data.id;	
		  var rolesId = data.roleIdList;
		 
	        
 
		  if(layEvent === 'edit'){ //修改身份
			 
			  $.post('/back/base/userIdentityEditUI',{"id":id}, function(str){
//				  console.log(id);
				  layer.open({
				    type: 1,
				    content: str,
				    title:'修改用户身份'//注意，如果str是object，那么需要字符拼接。
				  });
				  form.render();
				});
		  }else if(layEvent ==='default'){
			  var id = $(this).attr("data").replace("d_", "");		
				try{
					backManagerService.setDefaultUserIdentity(id);
					layer.alert("设置成功！",function(){
						location.href = ctx + "/back/base/userIdentityList";
					});				
				} catch (e) {
					layer.confirm("设置失败:"+e.message);
				}
		  }
	});
	
	//表单提交事件
	var IsRun=false;
	 form.on('submit(identify_sumit)', function(data){
		 if(!IsRun){
			 
			  var roleList = [];
		         for(var name in data.field){//遍历对象属性名  				
			        if(name.indexOf("role")!=-1){
//				        roleList += name.substring(5,name.indexOf("]"))+",";
				        roleList.push(name.substring(5,name.indexOf("]")))
				        delete data.field[name];
			         }		
			     }  
		         if(roleList==""){
			        layer.msg("请至少选择一个角色！");
			        return false;
		         }
	          data.field.rolesId = roleList;
		     if(data.field.id =="" || data.field.id ==null){
		    	
		         backManagerService.userIdentityAdd(data.field);
		         alert("添加成功！")
			 }else{
	    	     backManagerService.userIdentityUpdate(data.field);
                 alert("修改成功！")
			 }
		 }
		 IsRun=true;
	 
	 });
});